<template>
  <!-- 企业管理 -->
  <div class="box">
    <!-- 搜索 新增 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="标签名称">
        <el-input v-model="formInline.user" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="province">
        <el-select
          v-model="formInline.region"
          placeholder="请选择"
          @keyup.enter="handleFilter"
          @change="handleProvince"
        >
          <el-option
            v-for="item in citySelect.province"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="无数据" value="shanghai"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业简称">
        <el-input v-model="formInline.user" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="searchFor">
        <el-button size="small" @click="submit">清除</el-button>
        <el-button
          size="small"
          type="primary"
          @click="onSubmit(formInline.user)"
          >搜索</el-button
        >
      </el-form-item>
      <span class="increase">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="success"
          @click="openDrawer('add')"
        >
          新增用户
        </el-button>
      </span>
    </el-form>
    <!-- 搜索 新增 -->
    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ total }} 条记录</span>
    </div>
    <!-- 记录 -->
    <!-- table 表格数据区域 -->
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column prop="id" label="序号" min-width="80"> </el-table-column>
      <el-table-column prop="number" label="企业编号" min-width="80">
      </el-table-column>
      <el-table-column prop="shortName" label="企业简称" min-width="80">
      </el-table-column>
      <el-table-column prop="tags" label="标签" min-width="80">
      </el-table-column>
      <el-table-column prop="creatorID" label="创建者" min-width="80">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" min-width="80">
      </el-table-column>
      <el-table-column prop="remarks" label="备注" min-width="80">
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="80">
      </el-table-column>
      <el-table-column prop="address" label="操作" width="180">
        <template #default="{ row }">
          <div class="actions">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="openDrawer('edit', row.id)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-check"
              circle
              plain
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="del(row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->
    <!-- 分页组件区域 -->
    <div class="block">
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryObj.page"
          :page-size="queryObj.pagesize"
          layout=" prev, pager, next, sizes, jumper"
          :page-sizes="[10, 20, 30, 50]"
          background
          :total="total"
        >
        </el-pagination>
      </el-row>
    </div>
    <!-- 分页组件区域 -->
    <!-- 弹窗组件 -->
    <CompanysAdd
      :dialogFormVisible.sync="visible"
      :id="currentId"
      :formBase="formBase"
    ></CompanysAdd>
    <!-- 弹窗组件 -->
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/companys.js'
import CompanysAdd from '../components/companys-add.vue'
import { provinces, citys } from '@/api/hmmm/citys.js'

export default {
  components: {
    CompanysAdd
  },
  data () {
    return {
      visible: false,
      drawerType: 'add', // 打开添加
      currentId: null,
      // 搜索
      formInline: {
        user: '',
        region: ''
      },
      list: [],
      formBase: {
        shortName: '', // 企业简称
        company: '', // 所属公司
        province: '', // 省份
        city: '', // 城市
        remarks: '', // 备注
        tags: '',
        isFamous: false // 是否名企
      },
      citySelect: {
        province: [],
        cityDate: []
      },
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10 // 控制每一页有几条数据
      },
      total: null // 总记录数
    }
  },

  created () {
    this.initTable()
  },

  methods: {
    // 点击操作选项打开dialod弹窗
    async openDrawer (type, id) {
      this.drawerType = type

      if (type === 'add') {
        this.visible = true // 显示弹层
        this.currentId = id
      } else if (type === 'edit') {
        this.visible = true // 显示弹层
        this.currentId = id

        // 因为vue更新是异步，在props的currentdId修改之前，已经创建了子组件发送了请求
        this.$nextTick(() => {
          // 调用子组件上的方法
          this.$refs.dialog.getDepartmentInfo()
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
          .then(async () => {
            await remove(id)
            this.$message.success('删除成功!')
            this.initTable()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    },
    // 获取table数据
    async initTable () {
      this.loading = true

      const { data } = await list(this.queryObj)
      console.log(data)

      this.list = [...data.items]
      this.total = data.counts

      this.loading = false
      console.log(this.list)
    },
    // // 点击删除图标删除对应的数据
    // async del (id) {
    //   console.log(id)
    //   try {
    //     await remove(id)
    //     this.$message.success('删除成功')
    //     this.initTable()
    //   } catch (error) {
    //     this.$message.error('删除失败')
    //   }
    // },
    // pageSize 改变时会触发
    handleSizeChange (pagesize) {
      console.log(`每页 ${pagesize} 条`)
      this.queryObj.pagesize = pagesize
      this.initTable()
    },

    // current当前页改变时会触发
    handleCurrentChange (page) {
      console.log(`当前页: ${page}`)
      // 改变参数对象中的当前页
      this.queryObj.page = page
      this.initTable()
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user) {
      // console.log(this.list.title)
      // console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.title)
        return item.title === user
      })
    },
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
    }
  }
}
</script>

<style scoped lang="less">
.box {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 20px;
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      display: flex;
      width: 25%;
      margin-right: 0;
      ::v-deep .el-form-item__content {
        display: flex;
        justify-content: space-between;
        width: 80%;
        .el-select {
          width: 100%;
          display: flex;
          justify-content: space-between;
          ::v-deep .el-input__inner {
            height: 32px;
            width: 100%;
            line-height: 32px;
            font-size: 14px;
          }
        }
      }
    }
    ::v-deep .searchFor {
      width: 25%;
      .el-form-item__content {
        width: 100%;
        display: flex;
        justify-content: flex-start;
        margin-left: 35px;
      }
    }
    ::v-deep .increase {
      height: 30px;
      width: 50%;
      display: flex;
      justify-content: flex-end;
      margin-right: 0;
    }
  }
  .record {
    width: 100%;
    margin-bottom: 20px;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    span {
      padding: 0 8px;
      font-size: 13px;
      line-height: 18px;
    }
  }
  .el-table {
    width: 100%;
    ::v-deep .el-table__cell {
      text-align: center;
    }
    ::v-deep .el-table__header-wrapper {
      border-bottom: 2px solid #e8e8e8;
      .hidden-columns {
        background-color: #fafafa;
      }
    }
  }
  .block {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    ::v-deep .el-pagination__jump {
      margin-left: 0;
    }
  }
}
</style>
